package com.dayatang.android.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;

import com.dayatang.android.Activity.R;
import com.dayatang.android.config.Config;
import com.dayatang.android.config.GridViewConfig;
import com.dayatang.android.config.ListViewDataConfig;

public class PMOWorkstationAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private Context context;
	private String serverAddress;
	private String username;
	
	private static final int[] layout = {R.layout.pmo_main,R.layout.index_activity};
	
	class PMOItemView {
		private GridView pmoGridView;
		private ListView pmoProjectStatusListView;
	}
	
	public PMOWorkstationAdapter(Context context, String serverAddress, String username) {
		this.username = username;
		this.serverAddress = serverAddress;
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}
	
	public int getCount() {
		return layout.length;
	}

	public Object getItem(int position) {
		return layout[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		PMOItemView itemView = null;
		
		if (convertView == null) {
			itemView = new PMOItemView();
			convertView = mInflater.inflate(layout[position], null);
			
			itemView.pmoGridView = (GridView) convertView.findViewById(R.id.index_gridview);
			itemView.pmoProjectStatusListView = (ListView) convertView.findViewById(R.id.pmo_projectstatud_listview);
			
			if (position == Config.ZERO) {
				String url = serverAddress + "/remote/workstation-pmo.action?username=" + username;
				String data = ListViewDataConfig.isNotProjectStatusData(url, context);
				if (data == null) {
					itemView.pmoProjectStatusListView.setAdapter(null);
				} else {
					itemView.pmoProjectStatusListView.setAdapter(new PMOProjectStatusListViewAdapter(context, ListViewDataConfig.getProjectStatusData(data, context)));
				}
			} else if (position == Config.ONE) {
				itemView.pmoGridView.setLayoutAnimation(AnimationUtils.loadLayoutAnimation(context, R.anim.layout_random_fade));
				itemView.pmoGridView.setAdapter(new MyGridViewAdapter(context,GridViewConfig.getGridViewList(Config.PMO,context),serverAddress));
				itemView.pmoGridView.setOnItemClickListener(GridViewConfig.itemClickListener);
			}
			
			convertView.setTag(itemView);
		} else {
			itemView = (PMOItemView) convertView.getTag();
		}
		
		return convertView;
	}
	
}
